/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.shop.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.shop.api.entity.PlatformRule;
import com.pig4cloud.pigx.shop.service.PlatformRuleService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 平台规则
 *
 * @author pigx code generator
 * @date 2023-12-31 12:01:25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/platform/rule")
@Tag(description = "platform_rule", name = "平台规则管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PlatformRuleController {

	private final PlatformRuleService pointRuleService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param pointRule 积分规则
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getPointRulePage(Page page, PlatformRule pointRule) {
		return R.ok(pointRuleService.page(page, Wrappers.query(pointRule)));
	}

	/**
	 * 通过id查询积分规则
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(pointRuleService.getById(id));
	}

	/**
	 * 新增平台规则
	 * @param pointRule 平台规则
	 * @return R
	 */
	@Operation(summary = "新增平台规则", description = "新增平台规则")
	@SysLog("新增平台规则")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_rule_add')")
	public R save(@RequestBody PlatformRule pointRule) {
		return R.ok(pointRuleService.save(pointRule));
	}

	/**
	 * 修改平台规则
	 * @param pointRule 平台规则
	 * @return R
	 */
	@Operation(summary = "修改平台规则", description = "修改平台规则")
	@SysLog("修改积分规则")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_rule_edit')")
	public R updateById(@RequestBody PlatformRule pointRule) {
		return R.ok(pointRuleService.updateById(pointRule));
	}

	/**
	 * 通过id删除平台规则
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除平台规则", description = "通过id删除平台规则")
	@SysLog("通过id删除平台规则")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_rule_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(pointRuleService.removeById(id));
	}

	/**
	 * 导出excel 表格
	 * @param pointRule 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_rule_export')")
	public List<PlatformRule> export(PlatformRule pointRule) {
		return pointRuleService.list(Wrappers.query(pointRule));
	}

	/**
	 * 按类型获取规则
	 * @return
	 */
	@Inner(false)
	@GetMapping("/c/getRuleByType")
	public R getRuleByType(PlatformRule platformRule) {
		return R.ok(pointRuleService
				.list(Wrappers.<PlatformRule>lambdaQuery().eq(PlatformRule::getRuleType, platformRule.getRuleType())));
	}

}
